<!DOCTYPE HTML>
<html lang="en-US" >
    
    <head>
        
        <meta charset="UTF-8">
        <title>序列化 | AKKA 2.3.6 Scala 文档</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 1.0.3">
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    
    
    
    <link rel="next" href="../chapter5/05_io.html" />
    
    
    <link rel="prev" href="../chapter5/03_remoting.html" />
    

        
    </head>
    <body>
        
        
<link rel="stylesheet" href="../gitbook/style.css">


        
    <div class="book"  data-level="5.4" data-basepath=".." data-revision="1442802141200">
    

<div class="book-summary">
    <div class="book-search">
        <input type="text" placeholder="Type to search" class="form-control" />
    </div>
    <ul class="summary">
        
    	
    	
    	

        

        
    
        
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                        <i class="fa fa-check"></i>
                        
                         Introduction
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1" data-path="chapter1/introduction.html">
            
                
                    <a href="../chapter1/introduction.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                         引言
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="1.1" data-path="chapter1/01_what_is_akka.html">
            
                
                    <a href="../chapter1/01_what_is_akka.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                         Akka是什么?
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1.2" data-path="chapter1/02_why_akka.html">
            
                
                    <a href="../chapter1/02_why_akka.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                         为什么使用Akka?
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1.3" data-path="chapter1/03_getting_started.html">
            
                
                    <a href="../chapter1/03_getting_started.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                         入门
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1.4" data-path="chapter1/04_the_obligatory_hello_world.html">
            
                
                    <a href="../chapter1/04_the_obligatory_hello_world.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                         必修的“Hello World”
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1.5" data-path="chapter1/05_usecase_and_deployment_scenarios.html">
            
                
                    <a href="../chapter1/05_usecase_and_deployment_scenarios.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                         用例和部署场景
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1.6" data-path="chapter1/06_examples_of_usecases_for_akka.html">
            
                
                    <a href="../chapter1/06_examples_of_usecases_for_akka.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                         Akka使用实例
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        
        <li class="chapter " data-level="2" data-path="chapter2/general.html">
            
                
                    <a href="../chapter2/general.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                         概述
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="2.1" data-path="chapter2/01_terminology_concepts.html">
            
                
                    <a href="../chapter2/01_terminology_concepts.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                         术语，概念
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2.2" data-path="chapter2/02_actor_systems.html">
            
                
                    <a href="../chapter2/02_actor_systems.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                         Actor系统
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2.3" data-path="chapter2/03_what_is_an_actor.html">
            
                
                    <a href="../chapter2/03_what_is_an_actor.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                         什么是Actor?
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2.4" data-path="chapter2/04_supervision_and_monitoring.html">
            
                
                    <a href="../chapter2/04_supervision_and_monitoring.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                         监管与监控
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2.5" data-path="chapter2/05_actor_references_paths_and_addresses.html">
            
                
                    <a href="../chapter2/05_actor_references_paths_and_addresses.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                         Actor引用, 路径与地址
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2.6" data-path="chapter2/06_location_transparency.html">
            
                
                    <a href="../chapter2/06_location_transparency.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                         位置透明性
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2.7" data-path="chapter2/07_akka_and_the_java_memory_model.html">
            
                
                    <a href="../chapter2/07_akka_and_the_java_memory_model.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.7.</b>
                        
                         Akka与Java内存模型
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2.8" data-path="chapter2/08_message_delivery_reliability.html">
            
                
                    <a href="../chapter2/08_message_delivery_reliability.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.8.</b>
                        
                         消息发送语义
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2.9" data-path="chapter2/09_configuration.html">
            
                
                    <a href="../chapter2/09_configuration.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.9.</b>
                        
                         配置
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        
        <li class="chapter " data-level="3" data-path="chapter3/actors.html">
            
                
                    <a href="../chapter3/actors.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                         Actors
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="3.1" data-path="chapter3/01_actors.html">
            
                
                    <a href="../chapter3/01_actors.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                         Actors
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.2" data-path="chapter3/02_typed_actors.html">
            
                
                    <a href="../chapter3/02_typed_actors.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                         有类型Actor
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.3" data-path="chapter3/03_fault_tolerance.html">
            
                
                    <a href="../chapter3/03_fault_tolerance.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                         容错
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="3.3.1" data-path="chapter3/03-1_fault_tolerance_sample.html">
            
                
                    <a href="../chapter3/03-1_fault_tolerance_sample.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.1.</b>
                        
                         容错示例
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        
        <li class="chapter " data-level="3.4" data-path="chapter3/04_dispatchers.html">
            
                
                    <a href="../chapter3/04_dispatchers.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                         调度器
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.5" data-path="chapter3/05_mailboxes.html">
            
                
                    <a href="../chapter3/05_mailboxes.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                         邮箱
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.6" data-path="chapter3/06_routing.html">
            
                
                    <a href="../chapter3/06_routing.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                         路由
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.7" data-path="chapter3/07_fsm.html">
            
                
                    <a href="../chapter3/07_fsm.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.7.</b>
                        
                         有限状态机(FSM)
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.8" data-path="chapter3/08_persistence.html">
            
                
                    <a href="../chapter3/08_persistence.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.8.</b>
                        
                         持久化
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.9" data-path="chapter3/09_testing_actor_systems.html">
            
                
                    <a href="../chapter3/09_testing_actor_systems.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.9.</b>
                        
                         测试Actor系统
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="3.9.1" data-path="chapter3/09_1_testkit-example.html">
            
                
                    <a href="../chapter3/09_1_testkit-example.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.9.1.</b>
                        
                         TestKit实例
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        
        <li class="chapter " data-level="3.10" data-path="chapter3/10_actor_dsl.html">
            
                
                    <a href="../chapter3/10_actor_dsl.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.10.</b>
                        
                         Actor DSL
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        
        <li class="chapter " data-level="4" data-path="chapter4/futures_and_agents.html">
            
                
                    <a href="../chapter4/futures_and_agents.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                         Futures与Agents
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="4.1" data-path="chapter4/01_futures.html">
            
                
                    <a href="../chapter4/01_futures.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                         Futures
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="4.2" data-path="chapter4/02_agents.html">
            
                
                    <a href="../chapter4/02_agents.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                         Agents
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        
        <li class="chapter " data-level="5" data-path="chapter5/networking.html">
            
                
                    <a href="../chapter5/networking.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                         网络
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="5.1" data-path="chapter5/01_cluster_cpecification.html">
            
                
                    <a href="../chapter5/01_cluster_cpecification.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                         集群规格
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="5.2" data-path="chapter5/02_cluster_usage.html">
            
                
                    <a href="../chapter5/02_cluster_usage.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                         集群用法
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="5.3" data-path="chapter5/03_remoting.html">
            
                
                    <a href="../chapter5/03_remoting.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                         远程
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter active" data-level="5.4" data-path="chapter5/04_serialization.html">
            
                
                    <a href="../chapter5/04_serialization.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                         序列化
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="5.5" data-path="chapter5/05_io.html">
            
                
                    <a href="../chapter5/05_io.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.5.</b>
                        
                         I/O
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="5.6" data-path="chapter5/06_using_tcp.html">
            
                
                    <a href="../chapter5/06_using_tcp.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.6.</b>
                        
                         使用TCP
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="5.7" data-path="chapter5/07_using_udp.html">
            
                
                    <a href="../chapter5/07_using_udp.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.7.</b>
                        
                         使用UDP
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="5.8" data-path="chapter5/08_zeromq.html">
            
                
                    <a href="../chapter5/08_zeromq.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.8.</b>
                        
                         ZeroMQ
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="5.9" data-path="chapter5/09_camel.html">
            
                
                    <a href="../chapter5/09_camel.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.9.</b>
                        
                         Camel
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        
        <li class="chapter " data-level="6" data-path="chapter6/utilities.html">
            
                
                    <a href="../chapter6/utilities.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                         实用工具
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="6.1" data-path="chapter6/01_event_bus.html">
            
                
                    <a href="../chapter6/01_event_bus.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                         事件总线
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="6.2" data-path="chapter6/02_logging.html">
            
                
                    <a href="../chapter6/02_logging.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                         日志
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="6.3" data-path="chapter6/03_scheduler.html">
            
                
                    <a href="../chapter6/03_scheduler.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                         调度器
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="6.4" data-path="chapter6/04_duration.html">
            
                
                    <a href="../chapter6/04_duration.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                         Duration
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="6.5" data-path="chapter6/05_circuit_breaker.html">
            
                
                    <a href="../chapter6/05_circuit_breaker.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.5.</b>
                        
                         线路断路器
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="6.6" data-path="chapter6/06_akka_extensions.html">
            
                
                    <a href="../chapter6/06_akka_extensions.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.6.</b>
                        
                         Akka扩展
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="6.7" data-path="chapter6/07_microkernel.html">
            
                
                    <a href="../chapter6/07_microkernel.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.7.</b>
                        
                         微内核
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        
        <li class="chapter " data-level="7" data-path="chapter7/howto_common_patterns.html">
            
                
                    <a href="../chapter7/howto_common_patterns.html">
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                         如何使用：常用模式
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="8" data-path="chapter8/experimental_modules.html">
            
                
                    <a href="../chapter8/experimental_modules.html">
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                         实验模块
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="8.1" data-path="chapter3/08_persistence.html">
            
                
                    <a href="../chapter3/08_persistence.html">
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                         持久化
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="8.2" data-path="chapter8/02_multi_node_testing.html">
            
                
                    <a href="../chapter8/02_multi_node_testing.html">
                        <i class="fa fa-check"></i>
                        
                            <b>8.2.</b>
                        
                         多节点测试
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="8.3" data-path="chapter8/03_actors.html">
            
                
                    <a href="../chapter8/03_actors.html">
                        <i class="fa fa-check"></i>
                        
                            <b>8.3.</b>
                        
                         Actors(使用Java的Lambda支持)
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="8.4" data-path="chapter8/04_fsm.html">
            
                
                    <a href="../chapter8/04_fsm.html">
                        <i class="fa fa-check"></i>
                        
                            <b>8.4.</b>
                        
                         FSM(使用Java的Lambda支持)
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="8.5" data-path="chapter8/05_external_contributions.html">
            
                
                    <a href="../chapter8/05_external_contributions.html">
                        <i class="fa fa-check"></i>
                        
                            <b>8.5.</b>
                        
                         外部贡献
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        
        <li class="chapter " data-level="9" data-path="chapter9/information_for_akka_developers.html">
            
                
                    <a href="../chapter9/information_for_akka_developers.html">
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                         Akka开发者信息
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="9.1" data-path="chapter9/01_building_akka.html">
            
                
                    <a href="../chapter9/01_building_akka.html">
                        <i class="fa fa-check"></i>
                        
                            <b>9.1.</b>
                        
                         构建Akka
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="9.2" data-path="chapter9/02_multi_jvm_testing.html">
            
                
                    <a href="../chapter9/02_multi_jvm_testing.html">
                        <i class="fa fa-check"></i>
                        
                            <b>9.2.</b>
                        
                         多JVM测试
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="9.3" data-path="chapter9/03_io_layer_design.html">
            
                
                    <a href="../chapter9/03_io_layer_design.html">
                        <i class="fa fa-check"></i>
                        
                            <b>9.3.</b>
                        
                         I/O层设计
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="9.4" data-path="chapter9/04_developer_guidelines.html">
            
                
                    <a href="../chapter9/04_developer_guidelines.html">
                        <i class="fa fa-check"></i>
                        
                            <b>9.4.</b>
                        
                         开发指南
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="9.5" data-path="chapter9/05_documentation_guidelines.html">
            
                
                    <a href="../chapter9/05_documentation_guidelines.html">
                        <i class="fa fa-check"></i>
                        
                            <b>9.5.</b>
                        
                         文档指南
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="9.6" data-path="chapter9/06_team.html">
            
                
                    <a href="../chapter9/06_team.html">
                        <i class="fa fa-check"></i>
                        
                            <b>9.6.</b>
                        
                         团队
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        
        <li class="chapter " data-level="10" data-path="chapter10/project_information.html">
            
                
                    <a href="../chapter10/project_information.html">
                        <i class="fa fa-check"></i>
                        
                            <b>10.</b>
                        
                         工程信息
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="10.1" data-path="chapter10/01_migration_guides.html">
            
                
                    <a href="../chapter10/01_migration_guides.html">
                        <i class="fa fa-check"></i>
                        
                            <b>10.1.</b>
                        
                         迁移指南
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="10.2" data-path="chapter10/02_issue_tracking.html">
            
                
                    <a href="../chapter10/02_issue_tracking.html">
                        <i class="fa fa-check"></i>
                        
                            <b>10.2.</b>
                        
                         问题追踪
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="10.3" data-path="chapter10/03_licenses.html">
            
                
                    <a href="../chapter10/03_licenses.html">
                        <i class="fa fa-check"></i>
                        
                            <b>10.3.</b>
                        
                         许可证
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="10.4" data-path="chapter10/04_sponsors.html">
            
                
                    <a href="../chapter10/04_sponsors.html">
                        <i class="fa fa-check"></i>
                        
                            <b>10.4.</b>
                        
                         赞助商
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="10.5" data-path="chapter10/05_project.html">
            
                
                    <a href="../chapter10/05_project.html">
                        <i class="fa fa-check"></i>
                        
                            <b>10.5.</b>
                        
                         项目
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        
        <li class="chapter " data-level="11" data-path="chapter11/additional_information.html">
            
                
                    <a href="../chapter11/additional_information.html">
                        <i class="fa fa-check"></i>
                        
                            <b>11.</b>
                        
                         附加信息
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="11.1" data-path="chapter11/01_faq.html">
            
                
                    <a href="../chapter11/01_faq.html">
                        <i class="fa fa-check"></i>
                        
                            <b>11.1.</b>
                        
                         常见问题
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="11.2" data-path="chapter11/02_books.html">
            
                
                    <a href="../chapter11/02_books.html">
                        <i class="fa fa-check"></i>
                        
                            <b>11.2.</b>
                        
                         图书
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="11.3" data-path="chapter11/03_other_language_bindings.html">
            
                
                    <a href="../chapter11/03_other_language_bindings.html">
                        <i class="fa fa-check"></i>
                        
                            <b>11.3.</b>
                        
                         其他语言绑定
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="11.4" data-path="chapter11/04_akka_in_osgi.html">
            
                
                    <a href="../chapter11/04_akka_in_osgi.html">
                        <i class="fa fa-check"></i>
                        
                            <b>11.4.</b>
                        
                         Akka与OSGi
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="11.5" data-path="chapter11/05_incomplete_list_of_http_frameworks.html">
            
                
                    <a href="../chapter11/05_incomplete_list_of_http_frameworks.html">
                        <i class="fa fa-check"></i>
                        
                            <b>11.5.</b>
                        
                         部分HTTP框架名单
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    


        
        <li class="divider"></li>
        <li>
            <a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Published using GitBook</a>
        </li>
        
    </ul>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header">
    <!-- Actions Left -->
    <a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>
    <a href="#" class="btn pull-left toggle-search" aria-label="Toggle search"><i class="fa fa-search"></i></a>
    
    <div id="font-settings-wrapper" class="dropdown pull-left">
        <a href="#" class="btn toggle-dropdown" aria-label="Toggle font settings"><i class="fa fa-font"></i>
        </a>
        <div class="dropdown-menu font-settings">
    <div class="dropdown-caret">
        <span class="caret-outer"></span>
        <span class="caret-inner"></span>
    </div>

    <div class="buttons">
        <button type="button" id="reduce-font-size" class="button size-2">A</button>
        <button type="button" id="enlarge-font-size" class="button size-2">A</button>
    </div>

    <div class="buttons font-family-list">
        <button type="button" data-font="0" class="button">Serif</button>
        <button type="button" data-font="1" class="button">Sans</button>
    </div>

    <div class="buttons color-theme-list">
        <button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">White</button>
        <button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">Sepia</button>
        <button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">Night</button>
    </div>
</div>

    </div>

    <!-- Actions Right -->
    
    <div class="dropdown pull-right">
        <a href="#" class="btn toggle-dropdown" aria-label="Toggle share dropdown"><i class="fa fa-share-alt"></i>
        </a>
        <div class="dropdown-menu font-settings dropdown-left">
            <div class="dropdown-caret">
                <span class="caret-outer"></span>
                <span class="caret-inner"></span>
            </div>
            <div class="buttons">
                <button type="button" data-sharing="twitter" class="button">Twitter</button>
                <button type="button" data-sharing="google-plus" class="button">Google</button>
                <button type="button" data-sharing="facebook" class="button">Facebook</button>
                <button type="button" data-sharing="weibo" class="button">Weibo</button>
                <button type="button" data-sharing="instapaper" class="button">Instapaper</button>
            </div>
        </div>
    </div>
    

    
    <a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Share on Google Plus"><i class="fa fa-google-plus"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Share on Facebook"><i class="fa fa-facebook"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Share on Twitter"><i class="fa fa-twitter"></i></a>
    
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >AKKA 2.3.6 Scala 文档</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-gitbook_129">
                    
                        <h1 id="">序列化</h1>
<blockquote>
<p>注：本节未经校验，如有问题欢迎提issue</p>
</blockquote>
<p>Akka 提供了内置的支持序列化的扩展, 你可以使用内置的序列化功能，也可以自己写一个.</p>
<p>内置的序列化功能被Akka内部用来序列化消息，你也可以用它做其它的序列化工作。</p>
<h3 id="">用法</h3>
<h5 id="">配置</h5>
<p>为了让 Akka 知道对什么任务使用哪个<code>Serializer</code>, 你需要编辑你的 <a href="../chapter2/09_configuration.html">配置文件</a>, 在 “akka.actor.serializers”一节将名称绑定为<code>akka.serialization.Serializer</code>的实现，像这样:</p>
<pre><code>akka {
  actor {
    serializers {
      java = &quot;akka.serialization.JavaSerializer&quot;
      proto = &quot;akka.remote.serialization.ProtobufSerializer&quot;
      myown = &quot;docs.serialization.MyOwnSerializer&quot;
    }
  }
}
</code></pre><p>在将名称与<code>Serializer</code>的不同实现绑定后，你需要指定哪些类的序列化使用哪种<code>Serializer</code>, 这部分配置写在“akka.actor.serialization-bindings”一节中:</p>
<pre><code>akka {
  actor {
    serializers {
      java = &quot;akka.serialization.JavaSerializer&quot;
      proto = &quot;akka.remote.serialization.ProtobufSerializer&quot;
      myown = &quot;docs.serialization.MyOwnSerializer&quot;
    }

    serialization-bindings {
      &quot;java.lang.String&quot; = java
      &quot;docs.serialization.Customer&quot; = java
      &quot;com.google.protobuf.Message&quot; = proto
      &quot;docs.serialization.MyOwnSerializable&quot; = myown
      &quot;java.lang.Boolean&quot; = myown
    }
  }
}
</code></pre><p>你只需要指定消息的接口或抽象基类. 当消息实现了配置中多个类时，为避免歧义, 将使用最具体的类, 即对其它所有配置指定类，它都是子类的那一个. 如果这个条件不满足, 例如因为同时配置<code>java.io.Serializable</code>和<code>MyOwnSerializable</code>，而彼此都不是对方的子类型，将生成一个警告。</p>
<p>Akka 缺省提供使用 <code>java.io.Serializable</code>和 <a href="http://code.google.com/p/protobuf/" target="_blank">protobuf</a> <code>com.google.protobuf.GeneratedMessage</code> 的序列化工具 (后者仅当定义了对akka-remote模块的依赖时才有), 所以通常你不需要添加这两种配置; 由于 <code>com.google.protobuf.GeneratedMessage</code> 实现了 <code>java.io.Serializable</code>, 在不特别指定的情况下，protobuf 消息将总是使用 protobuf 协议来做序列化. 要禁止缺省的序列化工具，将其对应的类型设为 “none”:</p>
<pre><code>akka.actor.serialization-bindings {
  &quot;java.io.Serializable&quot; = none
}
</code></pre><h3 id="">确认</h3>
<p>如果你希望确认你的消息是可以被序列化的，你可以打开这个配置项:</p>
<pre><code>akka {
  actor {
    serialize-messages = on
  }
}
</code></pre><blockquote>
<p>警告</p>
<p>我们推荐只在运行测试代码的时候才打开这个选项. 在其它的场景打开它完全没有道理.</p>
</blockquote>
<p>如果你希望确认你的 Props 可以被序列化, 你可以打开这个配置项:</p>
<pre><code>akka {
  actor {
    serialize-creators = on
  }
}
</code></pre><blockquote>
<p>警告</p>
<p>我们推荐只在运行测试代码的时候才打开这个选项. 在其它的场景打开它完全没有道理.</p>
</blockquote>
<h5 id="">通过代码</h5>
<p>如果你希望通过代码使用 Akka Serialization来进行序列化/反序列化, 以下是一些例子:</p>
<pre><code class="lang-scala"><span class="hljs-keyword">import</span> akka.actor.{ <span class="hljs-type">ActorRef</span>, <span class="hljs-type">ActorSystem</span> }
<span class="hljs-keyword">import</span> akka.serialization._
<span class="hljs-keyword">import</span> com.typesafe.config.<span class="hljs-type">ConfigFactory</span>

    <span class="hljs-function"><span class="hljs-keyword">val</span> <span class="hljs-title">system</span> =</span> <span class="hljs-type">ActorSystem</span>(<span class="hljs-string">"example"</span>)

    <span class="hljs-comment">// Get the Serialization Extension</span>
    <span class="hljs-function"><span class="hljs-keyword">val</span> <span class="hljs-title">serialization</span> =</span> <span class="hljs-type">SerializationExtension</span>(system)

    <span class="hljs-comment">// Have something to serialize</span>
    <span class="hljs-function"><span class="hljs-keyword">val</span> <span class="hljs-title">original</span> =</span> <span class="hljs-string">"woohoo"</span>

    <span class="hljs-comment">// Find the Serializer for it</span>
    <span class="hljs-function"><span class="hljs-keyword">val</span> <span class="hljs-title">serializer</span> =</span> serialization.findSerializerFor(original)

    <span class="hljs-comment">// Turn it into bytes</span>
    <span class="hljs-function"><span class="hljs-keyword">val</span> <span class="hljs-title">bytes</span> =</span> serializer.toBinary(original)

    <span class="hljs-comment">// Turn it back into an object</span>
    <span class="hljs-function"><span class="hljs-keyword">val</span> <span class="hljs-title">back</span> =</span> serializer.fromBinary(bytes, manifest = <span class="hljs-type">None</span>)

    <span class="hljs-comment">// Voilá!</span>
    back should be(original)
</code></pre>
<p>更多信息请见 <code>akka.serialization._</code> 的 <code>ScalaDoc</code></p>
<h3 id="">自定义</h3>
<p>如果你希望创建自己的 <code>Serializer</code>, 应该已经看到配置例子中的 <code>docs.serialization.MyOwnSerializer</code> 了吧?</p>
<h5 id="-serializer">创建新的 Serializer</h5>
<p>首先你需要为你的 <code>Serializer</code> 写一个类定义，像这样:</p>
<pre><code class="lang-scala"><span class="hljs-keyword">import</span> akka.actor.{ <span class="hljs-type">ActorRef</span>, <span class="hljs-type">ActorSystem</span> }
<span class="hljs-keyword">import</span> akka.serialization._
<span class="hljs-keyword">import</span> com.typesafe.config.<span class="hljs-type">ConfigFactory</span>

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyOwnSerializer</span> <span class="hljs-keyword"><span class="hljs-keyword">extends</span></span> <span class="hljs-title">Serializer</span> {</span>

  <span class="hljs-comment">// This is whether "fromBinary" requires a "clazz" or not</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">includeManifest</span>:</span> <span class="hljs-type">Boolean</span> = <span class="hljs-literal">false</span>

  <span class="hljs-comment">// Pick a unique identifier for your Serializer,</span>
  <span class="hljs-comment">// you've got a couple of billions to choose from,</span>
  <span class="hljs-comment">// 0 - 16 is reserved by Akka itself</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">identifier</span> =</span> <span class="hljs-number">1234567</span>

  <span class="hljs-comment">// "toBinary" serializes the given object to an Array of Bytes</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">toBinary</span>(</span>obj: <span class="hljs-type">AnyRef</span>): <span class="hljs-type">Array</span>[<span class="hljs-type">Byte</span>] = {
    <span class="hljs-comment">// Put the code that serializes the object here</span>
    <span class="hljs-comment">// ... ...</span>
  }

  <span class="hljs-comment">// "fromBinary" deserializes the given array,</span>
  <span class="hljs-comment">// using the type hint (if any, see "includeManifest" above)</span>
  <span class="hljs-comment">// into the optionally provided classLoader.</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">fromBinary</span>(</span>bytes: <span class="hljs-type">Array</span>[<span class="hljs-type">Byte</span>],
                 clazz: <span class="hljs-type">Option</span>[<span class="hljs-type">Class</span>[_]]): <span class="hljs-type">AnyRef</span> = {
    <span class="hljs-comment">// Put your code that deserializes here</span>
    <span class="hljs-comment">// ... ...</span>
  }
}
</code></pre>
<p>然后你只需要做填空，在 <a href="../chapter2/09_configuration.html">配置文件</a> 中将它绑定到一个名称， 然后列出需要用它来做序列化的类即可.</p>
<h5 id="actor">Actor引用的序列化</h5>
<p>所有的 ActorRef 都是用 JavaSerializer进行序列化的, 但如果你写了自己的serializer， 你可能想知道如何正确对它们进行序列化和反序列化。在一般情况下，要使用的本地地址取决于作为序列化信息收件人的远程地址的类型。像这样使用<code>Serialization.serializedActorPath(actorRef)</code>：</p>
<pre><code class="lang-scala"><span class="hljs-keyword">import</span> akka.actor.{ <span class="hljs-type">ActorRef</span>, <span class="hljs-type">ActorSystem</span> }
<span class="hljs-keyword">import</span> akka.serialization._
<span class="hljs-keyword">import</span> com.typesafe.config.<span class="hljs-type">ConfigFactory</span>

    <span class="hljs-comment">// Serialize</span>
    <span class="hljs-comment">// (beneath toBinary)</span>
    <span class="hljs-function"><span class="hljs-keyword">val</span> <span class="hljs-title">identifier</span>:</span> <span class="hljs-type">String</span> = <span class="hljs-type">Serialization</span>.serializedActorPath(theActorRef)

    <span class="hljs-comment">// Then just serialize the identifier however you like</span>

    <span class="hljs-comment">// Deserialize</span>
    <span class="hljs-comment">// (beneath fromBinary)</span>
    <span class="hljs-function"><span class="hljs-keyword">val</span> <span class="hljs-title">deserializedActorRef</span> =</span> extendedSystem.provider.resolveActorRef(identifier)
    <span class="hljs-comment">// Then just use the ActorRef</span>
</code></pre>
<p>这是假定序列化发生在通过远程传输发送消息的上下文中。不过有一些序列化有其他用途，例如在actor应用程序之外存储actor引用（数据库等）。在这种情况下，需要牢记的重要一点是，actor路径的地址部分决定actor如何被通信连通。存储一个本地actor路径可能是正确的选择，如果回取发生在相同的逻辑上下文，但当在不同的网络主机上反序列化时它还不够： 为此，它将需要包括系统的远程传输地址。一个actor系统并不局限于只有一个远程运输，使得这个问题变得更有意思。当向<code>remoteAddr</code>发送消息时，要找出恰当的地址，你可以像下面这样使用<code>ActorRefProvider.getExternalAddressFor(remoteAddr)</code>：</p>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">ExternalAddress</span> <span class="hljs-keyword"><span class="hljs-keyword">extends</span></span> <span class="hljs-title">ExtensionKey</span>[</span><span class="hljs-type">ExternalAddressExt</span>]

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ExternalAddressExt</span>(</span>system: <span class="hljs-type">ExtendedActorSystem</span>) <span class="hljs-keyword">extends</span> <span class="hljs-type">Extension</span> {
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">addressFor</span>(</span>remoteAddr: <span class="hljs-type">Address</span>): <span class="hljs-type">Address</span> =
    system.provider.getExternalAddressFor(remoteAddr) getOrElse
      (<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-type">UnsupportedOperationException</span>(<span class="hljs-string">"cannot send to "</span> + remoteAddr))
}

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">serializeTo</span>(</span>ref: <span class="hljs-type">ActorRef</span>, remote: <span class="hljs-type">Address</span>): <span class="hljs-type">String</span> =
  ref.path.toSerializationFormatWithAddress(<span class="hljs-type">ExternalAddress</span>(extendedSystem).
    addressFor(remote))
</code></pre>
<blockquote>
<p>注意</p>
<p>如果地址并没有<code>host</code>和<code>port</code>组件时<code>ActorPath.toSerializationFormatWithAddress</code>不同于<code>toString</code>，即它只为本地地址插入地址信息。</p>
<p><code>toSerializationFormatWithAddress</code> 还为actor添加了唯一 id，当actor终止，然后又按照相同名称重新创建时，该id将改变。将消息发送到指向那个老actor的引用，将不会传递给新的actor。如果你不想要这种行为，例如在长期存储引用的情况下，你可以使用不包括的唯一id的 <code>toStringWithAddress</code>。</p>
</blockquote>
<p>这就要求你至少知道哪种类型的地址将被反序列化生成的actor引用的系统支持；如果你手头没有具体的地址，你可以创建一个虚拟使用正确协议的地址 <code>Address(protocol, &quot;&quot;, &quot;&quot;, 0)</code>（假定所用的实际传输与Akka的<code>RemoteActorRefProvider</code>是相兼容的）。</p>
<p>也有一个用来支持集群的默认远程地址（并且典型系统只有这一个）；你可以像这样获取它：</p>
<pre><code class="lang-scala"><span class="hljs-class"><span class="hljs-keyword">object</span> <span class="hljs-title">ExternalAddress</span> <span class="hljs-keyword"><span class="hljs-keyword">extends</span></span> <span class="hljs-title">ExtensionKey</span>[</span><span class="hljs-type">ExternalAddressExt</span>]

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ExternalAddressExt</span>(</span>system: <span class="hljs-type">ExtendedActorSystem</span>) <span class="hljs-keyword">extends</span> <span class="hljs-type">Extension</span> {
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">addressForAkka</span>:</span> <span class="hljs-type">Address</span> = system.provider.getDefaultAddress
}

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">serializeAkkaDefault</span>(</span>ref: <span class="hljs-type">ActorRef</span>): <span class="hljs-type">String</span> =
  ref.path.toSerializationFormatWithAddress(<span class="hljs-type">ExternalAddress</span>(theActorSystem).
    addressForAkka)
</code></pre>
<h5 id="actor">Actor的深度序列化</h5>
<p>做内部actor状态深度序列化，推荐的方法是使用<a href="../chapter3/08_persistence.html">Akka持久化</a>。</p>
<h3 id="-java-">关于 Java 序列化</h3>
<p>如果在做Java序列化时没有使用<code>JavaSerializer</code>, 你必须保证在动态变量<code>JavaSerializer.currentSystem</code>中提供一个有效的 <code>ExtendedActorSystem</code> . 它用在读取<code>ActorRef</code>表示时将其字符串表示转换成实际的引用. 动态变量<code>DynamicVariable</code> 是一个 thread-local变量，所以在反序列化任何可能包含actor引用的数据时要保证这个变量有值.</p>
<h3 id="-akka-serializers">外部 Akka Serializers</h3>
<p><a href="https://github.com/romix/akka-protostuff-serialization" target="_blank">Akka-protostuff by Roman Levenstein</a></p>
<p><a href="https://github.com/romix/akka-quickser-serialization" target="_blank">Akka-quickser by Roman Levenstein</a></p>
<p><a href="https://github.com/romix/akka-kryo-serialization" target="_blank">Akka-kryo by Roman Levenstein</a></p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../chapter5/03_remoting.html" class="navigation navigation-prev " aria-label="Previous page: 远程"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../chapter5/05_io.html" class="navigation navigation-next " aria-label="Next page: I/O"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="https://cdn.mathjax.org/mathjax/2.4-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-mathjax/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontSettings":{"theme":null,"family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
